import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Map } from 'immutable';

import ClassifyItem from 'components/Thing/ClassifyItem';
import TreeNode from 'components/Common/TreeNode';
import { CommonActions, ThingsActions } from 'store/actionCreators';
import { type State } from 'store';

type Props = {
  classifiesData: any,
  openedIds: Array<string>,
  editableIds: Array<string>,
};
class ClassifiesContainer extends Component<Props> {
  constructor(props: Props) {
    super(props);
    this.classifyEditable = React.createRef();
  }
  nodeItemRedner = (item, isLeaf, isOpen) => {
    return (
      <ClassifyItem
        data={item}
        isOpen={isOpen}
        classifyType={item.classifyType}
        actionType={item.actionType}
        editableIds={this.props.editableIds}
        editableInputRef={this.classifyEditable}
        onEditableItemDoubleClick={(sid) => {
          CommonActions.enableEditable(sid);
        }}
        onEditableItemOutClick={(sid) => {
          CommonActions.diableEditable(sid);
        }}
        onEditablItemChange={({ id, value }) => {
          ThingsActions.changeClassify({ id, value });
        }}
        onClickClassifyIcon={(sid) => {
          CommonActions.changeTreeOpen(sid);
        }}
      />
    );
  };
  render() {
    const { classifiesData, openedIds, openedRange } = this.props;
    return (
      <TreeNode
        datas={classifiesData}
        openedIds={openedIds}
        openedRange={openedRange}
        treeType="classify"
        NodeRender={(item, isLeaf, isOpen) => {
          // console.log(`${item.id}:isleaf--${isLeaf}:isopen--${isOpen}`);
          return this.nodeItemRedner(item, isLeaf, isOpen);
        }}
      />
    );
  }
}

const mapStateToProps = ({ common, things }: State) => ({
  classifiesData: things.classify.data,
  openedIds: common.tree.openedIds,
  editableIds: common.editable.EditableIds,
  openedRange: things.classify.isopenRange,
});

const mapDispatchToProps = dispatch => ({});

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(ClassifiesContainer);
